package cn.lj.logistics.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.tags.EditorAwareTag;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.lj.logistics.pojo.MsgObject;
import cn.lj.logistics.pojo.Role;
import cn.lj.logistics.pojo.RoleExample;
import cn.lj.logistics.pojo.Permission;
import cn.lj.logistics.pojo.PermissionExample;
import cn.lj.logistics.pojo.PermissionExample.Criteria;
import cn.lj.logistics.service.RoleService;
import cn.lj.logistics.service.PermissionService;
import net.sf.jsqlparser.statement.delete.Delete;

@Controller
@RequestMapping("/permission")
public class PermissionController {
     
	@Autowired
	private PermissionService permissionService;
	
	
	//编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m,Long permissionId) {
		
		//修改操作
		if (permissionId!=null) {
			// 查询出当前用户的数据，共享给编辑页面，以供修改回显数据
			Permission permission = permissionService.selectByPrimaryKey(permissionId);
			m.addAttribute("permission",permission);
		}
		
		// 查询出所有权限的父权限（type=menu的权限就是父权限）
	PermissionExample example=new PermissionExample();
	Criteria criteria = example.createCriteria();
	criteria.andTypeEqualTo("menu");
	List<Permission> parents = permissionService.selectByExample(example);
		m.addAttribute("parents", parents);
		return "permissionEdit";
		
	}
	

	
	//新增功能
		@RequestMapping("/insert")
		@ResponseBody
		public MsgObject insert(Permission permission) {
			
			
		
		int row = permissionService.insert(permission);
			MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！！");
			if (row==1) {
				mo=MsgObject.createMO(1, "新增数据成功");
			}
			return mo;
			
		}
		
		//修改功能
				@RequestMapping("/update")
				@ResponseBody
				public MsgObject update(Permission permission) {
				
				
				int row = permissionService.updateByPrimaryKeySelective(permission);
					MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！！");
					if (row==1) {
						mo=MsgObject.createMO(1, "修改数据成功");
					}
					return mo;
					
				}
		
	
	//删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long permissionId) {
		/*
		 * 删除之前检查对应的权限id是否还有子权限
		 * 
		 * 查询 parentId = 要删除的权限id  条件
		 */
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(permissionId);
		
		List<Permission> children = permissionService.selectByExample(example);
		

		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");
		
		if (children.size()>0) {
			mo = MsgObject.createMO(0, "此权限还有子权限，不能删除！");
			return mo;
		}
		
		int row = permissionService.deleteByPrimaryKey(permissionId);
	
		if (row==1) {
			mo=MsgObject.createMO(1, "删除数据成功");
		}
		return mo;
		
	}
	

	
	@RequestMapping("/permissionPage")
	public String permissionPage() {
		
		return "permissionPage";
		
	}
	

	@RequestMapping("/getAllPermission")
	@ResponseBody
	public List<Permission> getAllPermission(){
		PermissionExample example = new PermissionExample();
		List<Permission> permissions = permissionService.selectByExample(example );
		return permissions;
	}
	
	
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<Permission> list(@RequestParam(defaultValue = "1")Integer pageNum,
			               @RequestParam(defaultValue = "10")Integer pageSize ,String keyword){
		System.out.println("keyword:"+ keyword);
		//开始分页
		PageHelper.startPage(pageNum, pageSize);
		PermissionExample example=new PermissionExample();
		
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
			Criteria criteria1 = example.createCriteria();
		criteria1.andNameLike("%"+keyword+"%"); 
		
		Criteria criteria2 = example.createCriteria();
		criteria1.andParentNameLike("%"+keyword+"%");
	
		example.or(criteria2);
		}
		List<Permission> list = permissionService.selectByExample(example);
		
		return  new PageInfo<Permission>(list);
		
	}
}
